package walker

import (
	"fmt"
	"iptv_server/v3/src/settings"
	"os"
	"strconv"

	// bili "iptv_server/v3/wallk/src/walker/processbili"
	"path"
	"strings"
)

type group struct {
	// homevideo_model.Category
	ID   int
	Name string
	Dir  string
	Pic  string

	SubId string

	Site      *site
	Vods      []*vod
	SubGroups []*group

	FollowedTo     int // 对数值 所指分组的 扩充!
	SubGroups_bili []*group
}

// 文件夹 名 = sub_someSoterStr_name.subId.others
//
//		要求上述 这个name 避免使用点; subId使用英文和数字
//	 这个传入的 参数 已经去掉了前缀  sub_someSoterStr_
func getTvOthers(s string) (name string, subId string, yes bool) {
	name = s
	subId = ""
	yes = false
	if !strings.HasSuffix(s, ".others") {
		return
	}
	ss := strings.Split(s, ".")
	if len(ss) < 3 {
		return
	}

	//ss[len(ss)-1]  == others
	subId = ss[len(ss)-2]
	if len(subId) == 0 {
		return
	}

	name = strings.Join(ss[:len(ss)-2], ".") //重新拼接.
	if len(name) == 0 {
		return
	}

	yes = true
	return
}

func (gp *group) writeGroupId() {
	os.WriteFile(path.Join(gp.Dir, "group_id.txt"), []byte(strconv.Itoa(gp.ID)), 0644) //不检查 error
}

func (gp *group) writeSubFolderId(id int) {
	os.WriteFile(path.Join(gp.Dir, "group_sub_id.txt"), []byte(strconv.Itoa(id)), 0644) //不检查 error
}

// 找到 vod目录
// // vods, sub_folders
func (gp *group) FindVods() {
	groupRootPath := gp.Dir

	//// 使用更 通用的 getTvOthers
	// if strings.HasSuffix(groupRootPath, ".bili.others") {
	// 	//name
	// 	//pic: __poster.jpg
	// 	//vod: __vod.json
	// 	gp.Name, _ = strings.CutSuffix(gp.Name, ".bili.others")
	// 	gp.SubId = "bili"
	// 	return
	// }

	// 第二层 处理;  第一层时 没有
	if name, subid, yes := getTvOthers(gp.Name); yes {
		gp.Name = name

		var id int
		if id1, ok := othersIds[subid]; ok {
			id = id1 + 1
		} else {
			id = 1 // 从1开始
		}
		othersIds[subid] = id

		// 更新 id, 这个是 others 内的 子id. 跟原来的 组ID不同!!!
		gp.writeSubFolderId(id)

		// gp.SubId = subid
		gp.SubId = fmt.Sprintf("%s/category/%s/%d", settings.UrlPath_TvOthers, subid, id)
		return
	}

	ds, _ := listDir2(groupRootPath)
	for _, d := range ds {
		dir := d.Name()

		if strings.HasSuffix(dir, "..off") {
			continue
		}

		if strings.HasPrefix(dir, "sub_") {
			//  nil site for subgroup
			name := strings.TrimRight(dir, "_")
			names := strings.Split(name, "_")
			name = names[len(names)-1]
			fmt.Printf("sub group name: %v\n", name)

			pa := path.Join(groupRootPath, dir)
			pic := getPicPath(pa, 2)

			// 第一层
			// if strings.HasSuffix(groupRootPath, ".bili.others") {
			// 	//name
			// 	//pic: __poster.jpg
			// 	//vod: __vod.json
			// 	name, _ = strings.CutSuffix(name, ".bili.others")
			// 	bili_gp := groupBuilder.NewGroup(name, pa, pic, siteNull)
			// 	gp.SubGroups_bili = append(gp.SubGroups_bili, bili_gp)
			// 	continue
			// }

			sub_gp := groupBuilder.NewGroup(name, pa, pic, siteNull)
			gp.SubGroups = append(gp.SubGroups, sub_gp)
			continue
		}

		//第一层是排序 or 子目录.  第二层开始 vod
		ds2, _ := listDir2(groupRootPath, dir)
		for _, d2 := range ds2 {
			dir2 := d2.Name()
			// vods
			vod_i := NewVod(path.Join(groupRootPath, dir, dir2), gp, 0)
			vod_i.Name = dir2
			vod_i.VideoTitleFilter = 0
			fmt.Printf("vod_i.Name: %v\n", vod_i.Name)
			//  json中的Name优先!  没有json 用此处的Name
			if name, yes := CutNeedTitleSuffix(dir2); yes {
				vod_i.Name = name
				vod_i.VideoTitleFilter = 1
			} else if name, yes := CutNeedTitleSuffix2(dir2); yes {
				vod_i.Name = name
				vod_i.VideoTitleFilter = 2
			}
			gp.Vods = append(gp.Vods, vod_i)
		}

	}
}
